﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using TracNghiemTinHocDaiCuong.Common;
using TracNghiemTinHocDaiCuong.BIZ;
using TracNghiemTinHocDaiCuong.ThucThe;

namespace TracNghiemTinHocDaiCuong
{
    public partial class KetQua : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            lblDiem.Text = Session["Diem"].ToString();
            int maDe = Methods.ConvertNumber(Session["maDe"].ToString());
            lvTest.DataSource = DeThi_CauHoiBIZ.LayDanhSach(maDe);
            lvTest.DataBind();
        }

        protected void lvTest_PagePropertiesChanged(object sender, EventArgs e)
        {
            lvTest.DataBind();
        }

        protected void dpTest_PreRender(object sender, EventArgs e)
        {
            string baiLam = "";
            if (Session["idNguoiDung"] != null)
            {
                int maDe = Methods.ConvertNumber(Session["maDe"].ToString());
                int maNguoiDung = (int)Session["idNguoiDung"];
                int maBL = (int)Session["MaBL"];
                baiLam = BaiLamBIZ.LayBaiLam(maBL, maNguoiDung, maDe);
            }
            else
            {
                baiLam = Methods.ConvertString(Session["BaiLam"].ToString());
            }
            foreach (ListViewItem item in lvTest.Items)
            {
                
                RadioButton rdoDapAnA = (RadioButton)item.FindControl("rdoDapAnA");
                RadioButton rdoDapAnB = (RadioButton)item.FindControl("rdoDapAnB");
                RadioButton rdoDapAnC = (RadioButton)item.FindControl("rdoDapAnC");
                RadioButton rdoDapAnD = (RadioButton)item.FindControl("rdoDapAnD");
                HiddenField hdfMaCH = (HiddenField)item.FindControl("hdfMaCH");
                char dapAn = DapAnNguoiDung(Methods.ConvertNumber(hdfMaCH.Value));
                switch (dapAn)
                {
                    case 'A':
                        rdoDapAnA.Checked = true;
                        break;
                    case 'B':
                        rdoDapAnB.Checked = true;
                        break;
                    case 'C':
                        rdoDapAnC.Checked = true;
                        break;
                    case 'D':
                        rdoDapAnD.Checked = true;
                        break;
                    default:
                        break;
                }
            }
        }

        public char DapAnNguoiDung(int maCH)
        {
            string baiLam = "";
            int maDe = Methods.ConvertNumber(Session["maDe"].ToString());
            if (Session["idNguoiDung"] != null)
            {
                int maNguoiDung = (int)Session["idNguoiDung"];
                int maBL = (int)Session["MaBL"];
                baiLam = BaiLamBIZ.LayBaiLam(maBL, maNguoiDung, maDe);
            }
            else
            {
                baiLam = Methods.ConvertString(Session["BaiLam"].ToString());
            }
            IList<string> DanhSachCauHoi = DeThi_CauHoiBIZ.DanhSachMaCH(maDe);
            int STT = 0;
            for (int i = 0; i < DanhSachCauHoi.Count; i++)
            {
                if (DanhSachCauHoi[i] == maCH.ToString())
                {
                    STT = i;
                    break;
                }
            }
            return baiLam[STT];
        }

        public char DapAnDung(int maCH)
        {
            int maDe = Methods.ConvertNumber(Session["maDe"].ToString());
            CauHoi cauHoi = CauHoiBIZ.TimKiemTheoId(maCH);
            return cauHoi.DapAn[0];
        }
    }
}